import {Component, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, ValidationErrors, Validators} from '@angular/forms';
import {BaseService} from '../../../shared/service/base.service';
import {ApiUrl} from '../../../config/api-url';

@Component({
  selector: 'app-merchant-update',
  templateUrl: './merchant-update.component.html',
  styleUrls: ['./merchant-update.component.less']
})
export class MerchantUpdateComponent implements OnInit {

  @Input()
  merchantUpdateVisible = false;
  @Output()
  settingMerchantUpdate: EventEmitter<any> = new EventEmitter<any>();

  isSpinning: any;

  title = '修改福利限额';

  validateForm!: FormGroup;


  // 模态框表单
  formModel: FormGroup;


  constructor(private fb: FormBuilder, private baseService: BaseService) {
  }

  initForm() {
    this.validateForm = this.fb.group({
      id: ['', [Validators.required]],
      welfareReturnCoinSum: ['', [Validators.required]],
      version: ['', [Validators.required]],
    });
  }

  ngOnInit(): void {
  }

  closeModal() {
    this.merchantUpdateVisible = false;
    this.settingMerchantUpdate.emit(this.merchantUpdateVisible);
    this.validateForm.reset({});
  }

  handleCancel(): void {
    this.closeModal();
  }

  loadingModal(data): void {
    this.initForm();
    this.findUser(data);
  }

  findUser(data) {
    this.baseService.get(ApiUrl.UserUrl + '/findById', data.id).subscribe(res => {
      this.isSpinning = false;
      if (res.status == 0) {
        this.validateForm.reset(res.data);
      } else {
        this.baseService.layer.messageError(res.msg);
        this.findUser(data);
      }
    });
  }

  handleOk() {
    this.isSpinning = true;
    if (!this.baseService.zorro.validateFormValidate(this.validateForm)) {
      this.isSpinning = false;
      return;
    }

    this.baseService.put(ApiUrl.UserUrl + '/updateWelfareReturnCoinSum', this.validateForm.value).subscribe(res => {
      this.isSpinning = false;
      if (res.status == 0) {
        this.baseService.layer.messageSuccess('修改成功');
        this.handleCancel();
      } else {
        this.baseService.layer.messageError(res.msg);
      }
    });
  }
}
